<?php
/**
 * Galdic project
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Galdic
 * @package    Galdic
 * @copyright  Copyright (c) 2009 vifito (http://vifito.eu)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * Admin_TermController
 * 
 * @package    Galdic
 * @copyright  Copyright (c) 2009 vifito (http://vifito.eu)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: TermController.php 1 2009-09-14 16:55:42Z vifito $
 */
class Admin_TermController extends Zend_Controller_Action
{
    /**
     * Check session
     */
    public function preDispatch()
    {
        $session = Zend_Registry::get('session');
        $actionName = $this->getRequest()->getActionName();
        
        if(!isset($session->username) && ($actionName!='login')) {
            $this->_redirect('/admin/user/login/');
        } elseif( isset($session->username) ) {
            $this->view->username = $session->username;
        }
    }

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->view->title = "Termos";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        $terms = new Admin_Model_DbTable_Terms();
        $rows = $terms->fetchAll(null, 'name ASC');
        
        // Pagination
        $paginator = Zend_Paginator::factory($rows);        
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        
        $this->view->assign('terms', $paginator);        
    }

    public function addAction()
    {
        // TODO: use Zend_Translate
        $this->view->title = "Novo termo";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        $form = new Admin_Form_Term();
        $this->view->form = $form;
        
        $formDefinition = new Admin_Form_DefinitionDialog();
        $this->view->formDefinition = $formDefinition;
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData)) {
                $term = new Admin_Model_DbTable_Terms();
                
                $data = $form->bindPostData($formData);
                
                // Date values
                $data['created'] = date('Y-m-d H:i:s');
                $data['updated'] = date('Y-m-d H:i:s');
                
                $term_id = $term->insert($data);                
                
                // Save json_definitions
                $definitions = new Admin_Model_DbTable_Definitions();
                $definitions->fromJson($formData['json_definitions'], $term_id);                
                
                $this->_redirect('/admin/term/');
            } else {
                $form->populate($formData);
            }            
        }
    }

    public function editAction()
    {
        $this->view->title = "Modificar datos";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        $form = new Admin_Form_Term();
        $this->view->form = $form;
        
        $formDefinition = new Admin_Form_DefinitionDialog();
        $this->view->formDefinition = $formDefinition;
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData)) {
                $term = new Admin_Model_DbTable_Terms();
                
                $data = $form->bindPostData($formData);
                $term_id = (int)$data['id'];
                unset($data['id']);
                
                // Date values (don't update created value)
                $data['updated'] = date('Y-m-d H:i:s');
                
                $term->update($data, '`id`='.$term_id);
                
                // Save json_definitions
                $definitions = new Admin_Model_DbTable_Definitions();
                $definitions->fromJson($formData['json_definitions'], $term_id); 
                
                $this->_redirect('/admin/term/'); 
            } else {
                $form->populate($formData);
            }            
        } else {
            $id = $this->_getParam('id', 0);
            if($id > 0) {
                $term = new Admin_Model_DbTable_Terms();                
                $form->populate( $term->fetchRow('`id`='.(int)$id)->toArray() );
                
                // JSON definitions for this term
                $definitions = new Admin_Model_DbTable_Definitions();
                $this->view->json_definitions = $definitions->toJson($id);
            } else {
                $this->_redirect('/admin/term/add/');
            }            
        }
    }

    public function deleteAction()
    {
        $this->view->title = "Eliminar termo";
        $this->view->headTitle($this->view->title, 'PREPEND');
        
        if($this->getRequest()->isPost()) {
            $action = $this->getRequest()->getPost('action');                        
            if($action == 'Si') {
                $id = $this->getRequest()->getPost('id');
                
                $definition = new Admin_Model_DbTable_Definitions();
                $definition->delete('`term_id`='.(int)$id);
                
                $term = new Admin_Model_DbTable_Terms();
                $term->delete('`id`='.(int)$id);                                
            }
                
            $this->_redirect('/admin/term/'); 
        } else {
            $id = $this->_getParam('id', 0);
            $term = new Admin_Model_DbTable_Terms();
            $this->view->term = $term->fetchRow('`id`='.(int)$id);
        }
    }


}









